<?php

namespace Api\Controller;

use Common\Controller\ApiController;

/**
 * 登录接口
 */
class LoginController extends ApiController {

    public function index() {
        $phone = I('post.phone', '');
        $pwd = I('post.pwd', '');

        if (empty($phone)) {
            apiReturn("手机号不能为空", 300);
        }

        if (empty($pwd)) {
            apiReturn("密码不能为空", 300);
        }
        if (!verifyPhone($phone)) {
            apiReturn("手机号格式不正确", 300);
        }

        $users = M('Users');

        //专业人员并且用户状态正常
        #$userInfo = $users->where("phone='{$phone}'")->field('id,name,nick_name,password,status')->find();
        $userInfo = $users->alias('u')
                ->join('__USER_GROUP_ACCESS__ uga ON u.id = uga.user_id')
                ->join('__USER_GROUP__ ug ON uga.group_id = ug.id')
                ->where("u.phone='{$phone}'")
                ->field('u.id,u.name,u.phone,u.password,u.status,uga.group_id,ug.name as groupname')
                ->find();
        if (empty($userInfo)) {
            apiReturn("手机号不存在", 300);
        }
        $pwd = md5($pwd);
        if ($userInfo['password'] != $pwd) {
            apiReturn('手机号或密码错误', 300);
        }
        if (!$userInfo['status']) {
            apiReturn('禁止登陆', 300);
        }
        apiReturn("登录成功", 200, $userInfo);
    }

}
